﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Annotations;

namespace OrganizeYourSearch
{
    class ListItemWithIcons:ListBoxItem
    {

        #region Global variables
                ImageSource iconSource;
                TextBlock textBlock;
                Image icon;
                MenuItem deleteLink;
                public MenuItem DeleteLink
                {
                    get { return deleteLink; }
                    set { deleteLink = value; }
                }
        #endregion Global variables
        #region Constructors and Destructors
        public ListItemWithIcons(string imagePath, string ToolTipText,Annotation ann)
        {
                 
            StackPanel stack = new StackPanel();
            stack.Orientation = Orientation.Horizontal;

            Content = stack;
            //Uncomment this code If you want to add an Image after the Node-HeaderText
            //textBlock = new TextBlock();
            //textBlock.VerticalAlignment = VerticalAlignment.Center;
            //stack.Children.Add(textBlock);
            icon = new Image();
            icon.VerticalAlignment = VerticalAlignment.Center;
            icon.Margin = new Thickness(0, 0, 4, 0);
            iconSource = CreateBitmapImageSource(imagePath);
            icon.Source = iconSource;
            stack.Children.Add(icon);
            //Add the HeaderText After Adding the icon
            textBlock = new TextBlock();
            textBlock.VerticalAlignment = VerticalAlignment.Center;
            int numchars = ToolTipText.Length > 25 ? 25 : ToolTipText.Length;
            textBlock.Text = ToolTipText.Substring(0, numchars);
            stack.Children.Add(textBlock);

            //Adding ContextMenu
            ContextMenu menu = new ContextMenu();
            deleteLink = new MenuItem();
            deleteLink.Header = "Delete";
            deleteLink.Tag = ann;
            menu.Items.Add(deleteLink);
            this.ContextMenu = menu;

            this.ToolTip = ToolTipText;
        }

        private ImageSource CreateBitmapImageSource(string imagePath)
        {
            BitmapImage myBitmapImage = new BitmapImage();

            // BitmapImage.UriSource must be in a BeginInit/EndInit block
            myBitmapImage.BeginInit();
            myBitmapImage.UriSource = new Uri(@imagePath,UriKind.RelativeOrAbsolute);

            // To save significant application memory, set the DecodePixelWidth or  
            // DecodePixelHeight of the BitmapImage value of the image source to the desired 
            // height or width of the rendered image. If you don't do this, the application will 
            // cache the image as though it were rendered as its normal size rather then just 
            // the size that is displayed.
            // Note: In order to preserve aspect ratio, set DecodePixelWidth
            // or DecodePixelHeight but not both.
            myBitmapImage.DecodePixelWidth = 16;
            myBitmapImage.EndInit();
            return myBitmapImage;

        }
        #endregion Constructors and Destructors
        #region Properties
        /// <summary>
        /// Gets/Sets the Selected Image for a TreeViewNode
        /// </summary>
        public ImageSource Icon
        {
            set
            {
                iconSource = value;
                icon.Source = iconSource;
            }
            get
            {
                return iconSource;
            }
        }
        #endregion Properties
        #region Event Handlers
        /// <summary>
        /// Event Handler on UnSelected Event
        /// </summary>
        /// <param name="args">Eventargs</param>
        protected override void OnUnselected(RoutedEventArgs args)
        {
            base.OnUnselected(args);
            icon.Source = iconSource;
        }
        /// <summary>
        /// Event Handler on Selected Event 
        /// </summary>
        /// <param name="args">Eventargs</param>
        protected override void OnSelected(RoutedEventArgs args)
        {
            base.OnSelected(args);
            icon.Source = iconSource;
        }

        /// <summary>
        /// Gets/Sets the HeaderText of TreeViewWithIcons
        /// </summary>
        public string HeaderText
        {
            set
            {
                textBlock.Text = value;
            }
            get
            {
                return textBlock.Text;
            }
        }
        
        #endregion Event Handlers
    }
}
